package com.lnsystem.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lnsystem.constants.LNSystemConstants;
import com.lnsystem.dao.AnalyticsEventDao;
import com.lnsystem.model.AnalyticsEvent;
import com.lnsystem.model.Stock;
import com.lnsystem.model.StockDailyData;
import com.lnsystem.service.impl.AnalyticsServiceImpl.PricePositionToPivots;
import com.sun.istack.internal.logging.Logger;

@Repository
public class AnalyticsEventDaoHibernateImpl implements AnalyticsEventDao {

	Logger logger = Logger.getLogger(AnalyticsEventDaoHibernateImpl.class);

	@Autowired
	SessionFactory sessionFactory;
	
	@Override
	public void flushAndClear() {
		Session session = sessionFactory.getCurrentSession();
        session.flush();
        session.clear();
	}

	@Override
	public List<AnalyticsEvent> getAllAnalyticsEvents() {
		
		Session session = sessionFactory.getCurrentSession();
		@SuppressWarnings("unchecked")
		List<AnalyticsEvent> eventList = session.createQuery("from AnalyticsEvent").list();

		// session.close();
		return eventList;
	}

	@Override
	public List<AnalyticsEvent> getAnalyticsEventsForStock(Stock stock) {
		Session session = sessionFactory.getCurrentSession();

		@SuppressWarnings("unchecked")
		List<AnalyticsEvent> eventList = session.createCriteria(AnalyticsEvent.class)
				.add(Restrictions.eq("stockId", stock)).list();
		if (eventList != null && eventList.size() > 0) {
			return eventList;
		}
		return null;
	}

	@Override
	public List<AnalyticsEvent> getAnalyticsEventsForStock(String symbol) {
		Session session = sessionFactory.getCurrentSession();

		@SuppressWarnings("unchecked")
		List<AnalyticsEvent> eventList = session.createCriteria(AnalyticsEvent.class)
				.add(Restrictions.eq("symbol", symbol)).list();
		if (eventList != null && eventList.size() > 0) {
			return eventList;
		}
		return null;
	}

	@Override
	public List<AnalyticsEvent> getAnalyticsEventsForADay(LocalDate date) {

		Session session = sessionFactory.getCurrentSession();
		@SuppressWarnings("unchecked")
		List<AnalyticsEvent> eventList = session.createCriteria(AnalyticsEvent.class)
				.add(Restrictions.eq("createdon", date.toDate())).list();
		if (eventList != null && eventList.size() > 0) {
			return eventList;
		}
		return null;
	}

	@Override
	public List<AnalyticsEvent> getAnalyticsEventsForADailyData(
			StockDailyData stockDailyData) {
		Session session = sessionFactory.getCurrentSession();

		@SuppressWarnings("unchecked")
		List<AnalyticsEvent> eventList = session.createCriteria(AnalyticsEvent.class)
				.add(Restrictions.eq("stockDailyId", stockDailyData)).list();
		if (eventList != null && eventList.size() > 0) {
			return eventList;
		}
		return null;
	}

	@Override
	public AnalyticsEvent getAnalyticsEventsByTypeForADailyData(
			StockDailyData stockDailyData, String eventType) {
		Session session = sessionFactory.getCurrentSession();

		@SuppressWarnings("unchecked")
		List<AnalyticsEvent> eventList = session.createCriteria(AnalyticsEvent.class)
				.add(Restrictions.eq("stockDailyId", stockDailyData))
				.add(Restrictions.eq("eventType", eventType))
				.list();
		if (eventList != null && eventList.size() > 0) {
			return eventList.get(0);
		}
		return null;
	}
	
	@Override
	public int saveAnalyticsEvent(AnalyticsEvent event) {
		Session session = sessionFactory.getCurrentSession();

		session.saveOrUpdate(event);
		Serializable id = session.getIdentifier(event);
		session.flush();

		return (Integer) id;
	}
	
	

}